﻿using System;
using System.Linq;
using Autofac;
using ComputerVision.BaseForm;
using ComputerVision.Common;
using ComputerVision.Data;
using ComputerVision.Services;
using ComputerVision.Services.User;
using QuanLyThongTinCoSo.Common;

namespace ComputerVision.PhieuDieuTraCoSoForm
{
    public partial class frmXuLyViPham : frmDetail
    {
        public Guid IdPhieu;
        private readonly IService<XuLyViPham> _xuLyViPhamService;
        private readonly IService<NhomHanhVi> _nhomHanhViService;
        private readonly IUserService<aspnet_Users> _userService;
        public frmXuLyViPham()
        {
            InitializeComponent();
            _xuLyViPhamService = Program.Container.Resolve<IService<XuLyViPham>>();
            _nhomHanhViService = Program.Container.Resolve<IService<NhomHanhVi>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            LoadNhomHanhVi();
            Text = @"Thêm mới Xử lý vi phạm";
            txtDoiTuongViPham.Focus();
        }

        public frmXuLyViPham(Guid id)
        {
            InitializeComponent();
            _xuLyViPhamService = Program.Container.Resolve<IService<XuLyViPham>>();
            _nhomHanhViService = Program.Container.Resolve<IService<NhomHanhVi>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            LoadNhomHanhVi();
            Text = @"Sửa Xử lý vi phạm";
            txtDoiTuongViPham.Focus();

            _fromtype = Constants.TypeForm.Edit;
            guid = id;

            var item = _xuLyViPhamService.Detail(id);
            if (item != null)
            {
                dtNgay.Value = item.Ngay;
                txtDoiTuongViPham.Text = item.DoiTuongViPham;
                txtNoiDung.Text = item.NoiDung;
                drlNhomHanhVi.SelectedValue = item.NhomHanhViId;
                txtCanhCao.Text = item.CanhCao;
                txtPhatTien.Text = item.PhatTien;
                dtNgayTamDinhChi.Value = item.NgayTamDinhChi;
                dtNgayPhucHoi.Value = item.NgayPhucHoi;
                txtDinhChi.Text = item.DinhChi;
                txtBienPhapXuLyKhac.Text = item.BienPhapXuLyKhac;
            }
        }

        #region Validation

        protected override bool Validator()
        {
            bool flag = true;

            errorProvider.Dispose();

            if (string.IsNullOrEmpty(txtDoiTuongViPham.Text))
            {
                errorProvider.SetError(txtDoiTuongViPham, "Bạn chưa nhập đối tượng vi phạm. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            return flag;
        }

        #endregion

        #region Insert
        protected override void Insert()
        {
            try
            {
                var model = new XuLyViPham
                {
                    Id = Guid.NewGuid(),
                    Ngay = dtNgay.Value.Date,
                    DoiTuongViPham = txtDoiTuongViPham.Text,
                    NoiDung = txtNoiDung.Text,
                    NhomHanhViId = Guid.Parse(drlNhomHanhVi.SelectedValue.ToString()),
                    CanhCao = txtCanhCao.Text,
                    PhatTien = txtPhatTien.Text,
                    NgayTamDinhChi = dtNgayTamDinhChi.Value.Date,
                    NgayPhucHoi = dtNgayPhucHoi.Value.Date,
                    DinhChi = txtDinhChi.Text,
                    BienPhapXuLyKhac = txtBienPhapXuLyKhac.Text,
                    PhieuDieuTraCoSoesId = this.IdPhieu,
                    Created = DateTime.Now,
                    CreatedBy = Constants.CurrentUser,
                    Modified = DateTime.Now,
                    ModifiedBy = Constants.CurrentUser
                };
                _xuLyViPhamService.Create(model);

            }
            catch (Exception ex)
            {
                AlertList.Error("Lỗi phát sinh khi tạo mới dữ liệu.");
            }
        }
        #endregion

        #region Edit

        protected override void Edit()
        {
            var item = _xuLyViPhamService.Detail(guid);
            if (item != null)
            {
                item.Ngay = dtNgay.Value.Date;
                item.DoiTuongViPham = txtDoiTuongViPham.Text;
                item.NoiDung = txtNoiDung.Text;
                item.NhomHanhViId = Guid.Parse(drlNhomHanhVi.SelectedValue.ToString());
                item.CanhCao = txtCanhCao.Text;
                item.PhatTien = txtPhatTien.Text;
                item.NgayTamDinhChi = dtNgayTamDinhChi.Value.Date;
                item.NgayPhucHoi = dtNgayPhucHoi.Value.Date;
                item.DinhChi = txtDinhChi.Text;
                item.BienPhapXuLyKhac = txtBienPhapXuLyKhac.Text;
                //item.PhieuDieuTraCoSoId = this.IdPhieu;
                item.Modified = DateTime.Now;
                item.ModifiedBy = Constants.CurrentUser;
                _xuLyViPhamService.Update(item);
            }
        }

        #endregion

        private void LoadNhomHanhVi()
        {
            drlNhomHanhVi.DataSource = _nhomHanhViService.ToList().ToList();
            drlNhomHanhVi.DisplayMember = "Ten";
            drlNhomHanhVi.ValueMember = "Id";
        }
    }
}
